package com.konsung.ui.defineview;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.konsung.healthfile.R;
import com.konsung.util.BroadcastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 加载进度窗
 */
public class LoadingDialog extends Dialog {
    private Context context;

    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.iv_loading)
    ImageView ivLoading;

    String content;

    /**
     * 构造
     * @param context 上下文
     * @param content 显示内容
     */
    public LoadingDialog(Context context, String content) {
        super(context, android.R.style.Theme_Translucent);
        this.context = context;
        this.content = content;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_loading);
        ButterKnife.bind(this);
        initData();
    }

    /**
     * 初始化参数
     */
    private void initData() {
        //设备号初始化
        tvContent.setText(content);
        Animation circleAnim = AnimationUtils.loadAnimation(context, R.anim.anim_round_rotate);
        //设置匀速旋转，在xml文件中设置会出现卡顿
        LinearInterpolator interpolator = new LinearInterpolator();
        circleAnim.setInterpolator(interpolator);
        ivLoading.setAnimation(circleAnim);
    }


    @Override
    public void show() {
        super.show();
        //设置显示dialog时窗体背景变暗
        BroadcastUtils.sendDarkForStartManage();
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = (int) (metrics.widthPixels * 0.9);
        lp.height = (int) (metrics.heightPixels * 0.9);
        lp.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        lp.dimAmount = 0.35f;
        getWindow().setAttributes(lp);
        initData();
    }


    @Override
    public void dismiss() {

        BroadcastUtils.sendNormalForStartManage();
        super.dismiss();
    }

    /**
     * 设置进度条加载的内容
     * @param content 加载的内容
     */
    public void setContent(String content) {
        tvContent.setText(content);
    }
}
